package com.liush.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liush.common.result.Result;
import com.liush.common.util.MD5;
import com.liush.model.controller.SysUser;
import com.liush.model.vo.SysUserQueryVo;
import com.liush.system.annotation.Log;
import com.liush.system.enums.BusinessType;
import com.liush.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author liush
 * @since 2023-02-25
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation(value = "获取分页列表")
    @GetMapping("/{pageNum}/{pageSize}")
    public Result index(
            @ApiParam(name = "pageNum", value = "当前页码", required = true)
            @PathVariable Long pageNum,
            @ApiParam(name = "pageSize", value = "每页记录数", required = true)
            @PathVariable Long pageSize,
            @ApiParam(name = "sysUserQueryVo", value = "查询对象", required = false)
                    SysUserQueryVo sysUserQueryVo
    ) {
        // 自定义方法
        /*Page<SysUser> page = new Page<>(pageNum, pageSize);
        Page<SysUser> pageUser = sysUserService.selectPageUser(page, sysUserQueryVo);*/

        // mybatis-plus 的方法
        /*Page<SysUser> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        String keyword = sysUserQueryVo.getKeyword();
        queryWrapper.between(createTimeBegin != null && createTimeEnd != null, SysUser::getCreateTime, createTimeBegin, createTimeEnd)
                .like(keyword != null, SysUser::getUsername, keyword)
                .or().like(keyword != null, SysUser::getName, keyword)
                .or().like(keyword != null, SysUser::getPhone, keyword);*/

        Page page = new Page(pageNum, pageSize);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        String keyword = sysUserQueryVo.getKeyword();
        queryWrapper.between(createTimeBegin != null && createTimeEnd != null, "create_time", createTimeBegin, createTimeEnd)
                .like(keyword != null, "username", keyword)
                .or().like(keyword != null, "name", keyword)
                .or().like(keyword != null, "phone", keyword)
        ;
        Page<SysUser> pageUser = sysUserService.page(page, queryWrapper);
        return Result.ok(pageUser);
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation(value = "根据id获取用户")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id) {
        System.out.println("用户id = " + id);
        SysUser user = sysUserService.getById(id);
        return Result.ok(user);
    }

    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @ApiOperation(value = "添加用户")
    @PostMapping("/save")
    public Result save(@RequestBody SysUser sysUser) {
        sysUser.setPassword(MD5.encrypt(sysUser.getPassword()));
        sysUserService.save(sysUser);
        return Result.ok();
    }

    @Log(title = "用户管理", businessType = BusinessType.UPDATE,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "更新用户")
    @PutMapping("/update")
    public Result updateById(@RequestBody SysUser user) {
        sysUserService.updateById(user);
        return Result.ok();
    }

    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        sysUserService.removeById(id);
        return Result.ok();
    }

    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "修改用户状态")
    @GetMapping("/updateStatus/{userId}/{status}")
    public Result updateStatus(
            @ApiParam(name = "userId", value = "用户id")
            @PathVariable Long userId,
            @ApiParam(name = "status", value = "状态码")
            @PathVariable Integer status
    ) {
        sysUserService.updateStatus(userId, status);
        return Result.ok();
    }
}
